<template>
	<view>
		<u-form  :model="apply" :rules="rules" ref="uForm" :errorType="errorType" >
			
			<view class="box">
				<view class="profily">
					<view style="margin-top: -20px;">
						<u-form-item :rightIconStyle="{color: '#888', fontSize: '50rpx'}" :label-position="labelPosition" label-width="100%" > <!-- icon：right-icon="kefu-ermai" -->
							<text style="color: red;">联系电话将保密，仅管理员和活动发起人可见</text>
						</u-form-item>
						
						<u-form-item :rightIconStyle="{color: '#888', fontSize: '50rpx'}" prop="username" :label-position="labelPosition" label="姓名" label-width="150" :required="true"> <!-- icon：right-icon="kefu-ermai" -->
							<u-input :border="border" placeholder="您的姓名" v-model="apply.username" type="text"></u-input>
						</u-form-item>
						
						<u-form-item :rightIconStyle="{color: '#888', fontSize: '50rpx'}" prop="phone"  :label-position="labelPosition" label="联系电话" label-width="150" :required="true">
							<u-input :border="border" placeholder="联系电话" v-model="apply.phone" type="text"></u-input>
						</u-form-item>
					</view>
				</view>
			</view>
			
		</u-form>
		<button class="button" @click="submit">提交</button>
		<view style="height: 30px;"></view>
	</view>
</template>

<script>
    export default {
        data() {
            return {
				apply: {
					username: '',
					phone: '',
					applyUid: this.$store.state.vuex_user.openId,
					infoId: '',
				},
				errorType: 'toast',
				labelPosition: 'left',
				rules: {
					username: [
						{
							required: true, 
							message: '姓名不能为空',
							trigger: ['blur', 'change'],
							transform(value) {
								return String(value);
							},
						}
					],
					phone: [
						{
							required: true, 
							message: '联系电话不能为空',
							trigger: ['blur', 'change'],
							transform(value) {
								return String(value);
							},
						}
					],
				},
				activityInfo: '',
            }
        },
        onLoad(params) {
        	//#ifdef H5
        	this.$u.toast("H5不支持此功能")
        	//#endif
        	
        	// #ifdef MP
        		if (!this.$u.utils.isLogin()) return
        	// #endif
        	
        	this.activityInfo = JSON.parse(decodeURIComponent(params.activityInfo))
			this.apply.infoId = this.activityInfo.id
        },
        methods: {
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						this.$u.api.applyAdd(this.apply).then(res=> {
							if(res.code==200) {
								// this.$u.route({
								// 	type: 'reLaunch',
								// 	url: 'pages/activity/activity_info?activityInfo=' + encodeURIComponent(JSON.stringify(this.activityInfo)),
								// })
								// uni.$emit("info", encodeURIComponent(JSON.stringify(this.activityInfo)))//这里可以传个空,也可以传值过去
								let pages = getCurrentPages(); // 当前页面
								let beforePage = pages[pages.length - 2]; // 上一页
								uni.navigateBack({
								    success: function() {
										//执行onLoad 没生效，写了监听器
								        beforePage.onLoad(encodeURIComponent(JSON.stringify(this.activityInfo))); // 执行上一页的onLoad方法
								    }
								});
								uni.showToast({
									icon: 'success',
									title:"报名成功"
								})
							} else {
								uni.showToast({
									icon: 'error',
									title:"报名失败"
								})
							}
						})
					} else {
						uni.showToast({
							icon: 'error',
							title:"请仔细填写内容哦"
						})
					}
				});
			},
			onPageScroll(e) {
				console.log(e)
				this.pageScrollTop = e.scrollTop
			},
        },
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
	}
</script>

<style lang="scss">
	.box {
		position: relative;
		padding-top: 13px;
		.profily {
			positio: absolute;
			width: 90%;
			// height: 545px;
			display: flex;
			border:1px solid #F7F7F7;
			border-radius: 10px;
			margin: 0 auto;
			top: -100upx;
			left: 5%;
			background: #FEFEFE;
			padding: 35upx;
			box-sizing: border-box;
			box-shadow: 2px 2px 5px #bebebe;
		}
	}	
	.button {
		margin-top: 30px;
		background-color: #193867;
		color:white;
		width: 380px;
		height: 40px;
		border:0;
		font-size: 16px;
		color: #f2f2f2;
		border-radius: 30px;
	}
</style>